package com.yexuejc.vertx.todolist.service;

import com.yexuejc.base.util.JsonUtil;
import com.yexuejc.vertx.todolist.consts.ServiceAddress;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.asyncsql.AsyncSQLClient;
import io.vertx.ext.asyncsql.MySQLClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @PackageName: com.yexuejc.vertx.todolist
 * @Description:
 * @author: maxf
 * @date: 2018/4/12 18:01
 */
public class ServiceHandler extends AbstractVerticle {
    Logger logger = LoggerFactory.getLogger(ServiceHandler.class);
    private AsyncSQLClient mySQLClient;

    @Override
    public void start() throws Exception {
        initDB();
        TodoSrv todoSrv = new TodoSrvImpl(mySQLClient);
        vertx.eventBus().consumer(ServiceAddress.GET_TODO).handler(context -> {
            todoSrv.getTodo(context.body(), res -> {
                context.reply(JsonUtil.obj2Json(res));
            });
        });
    }

    public void initDB() {
        JsonObject config = new JsonObject()
//                .put("url", "jdbc:mysql://localhost:3306/test?useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8")
                .put("host", "localhost")
                .put("port", 3306)
                .put("database", "test")
                .put("username", "root")
                .put("password", "root")
                .put("maxPoolSize", 30)
                .put("charset", "UTF-8");

//        client = JDBCClient.createShared(vertx, config);
        //        getUser();
        mySQLClient = MySQLClient.createShared(vertx, config);
    }

}
